﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 实体记录组件(copy过得实体组件列表)
/// </summary>
public class ECSEntityRecordCompoent : SingletonCompoent
{
    /// <summary>
    /// 实体组件信息copy列表
    /// </summary>
    public List<EntityRecordInfo> InfoList = new List<EntityRecordInfo>();

    public int InfoCount
    {
        get { return InfoList.Count; }
    }
    /// <summary>
    /// 是否匹配
    /// </summary>
    /// <param name="infos"></param>
    /// <param name="frame"></param>
    /// <param name="ID"></param>
    /// <param name="change"></param>
    /// <returns></returns>
    private bool Getagreement(EntityRecordInfo infos, int frame, int ID, EntityChangeType change)
    {
        if (infos.frame == frame && infos.id == ID && infos.changeType == change)
        {
            return true;
        }

        return false;
    }
    /// <summary>
    /// 实体组件记录列表中是否有指定帧指定id并且实体状态一致的组件copy（引用《非网络同步组件》）
    /// </summary>
    /// <param name="frame"></param>
    /// <param name="ID"></param>
    /// <param name="change"></param>
    /// <returns></returns>
    public bool GetRecordIsExist(int frame, int ID, EntityChangeType change)
    {
        for (int k = 0, j = InfoCount; k < j; k++)
       {

            if (Getagreement(InfoList[0], frame, ID, change))
            {
                return true;
            }
       }

        return false;
    }
    /// <summary>
    /// 是否有匹配的实体组件基类信息
    /// </summary>
    /// <param name="frame"></param>
    /// <param name="ID"></param>
    /// <param name="change"></param>
    /// <returns></returns>
    public EntityRecordInfo GetRecord(int frame, int ID, EntityChangeType change)
    {
        for (int i = 0, j = InfoCount; i < j; i++)
        {
            if (Getagreement(InfoList[i], frame, ID, change))
            {
                return InfoList[i];
            }
        }

        // return null;
        throw new Exception("没有匹配的实体组件copy信息,id+" + ID + "frame:" + frame + "changeType" + change);
    }

}
/// <summary>
/// 实体组件信息copy
/// </summary>
public struct EntityRecordInfo
{
    /// <summary>
    /// 哪一帧的信息
    /// </summary>
    public int frame;
    /// <summary>
    /// 哪个实体id
    /// </summary>
    public int id;
    /// <summary>
    /// 实体在这一帧是创建还是销毁状态
    /// </summary>
    public EntityChangeType changeType;
    /// <summary>
    /// 实体在这帧的组件列表copy
    /// </summary>
    public List<ECSCompoentBase> ComList;

    private ECSCompoentBase thisGo;
    private ECSMomentCompoentBase thisTo;
    /// <summary>
    /// 拷贝实体组件（网络组件进行copy，普通组件添加引用）
    /// </summary>
    /// <param name="entity"></param>
    public void SaveComp(ECSEntityBase entity)
    {
        ComList = ComList ?? new List<ECSCompoentBase>();

        for (int i = 0, j = entity.comps.Count; i < j; i++)
        {
            thisGo = entity.comps[i];
            if (thisGo == null)
            {
                continue;
            }
            //如果是需要网络同步的组件，那么只进行copy
            if (thisGo is ECSMomentCompoentBase)
            {
                thisTo = (ECSMomentCompoentBase)thisGo;
                ComList.Add(thisTo.DeepCopy());
            }//如果只是批图组件，那么直接拿到组件的引用
            else
            {
                ComList.Add(thisGo);
            }
        }

        thisGo = null;
        thisTo = null;
    }
}
/// <summary>
/// 实体改变类型
/// </summary>
public enum EntityChangeType
{
    None,
    Create,
    Destory,
}
